<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>node全局脚本原理解析 | 仲灏小栈</title>
    <meta name="generator" content="VuePress 1.9.5">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="专注web全栈学习与总结。JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github等技术文章。">
    <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
    <meta name="baidu-site-verification" content="code-5CGlqw1B4p">
    <meta name="google-site-verification" content="PbO8RiLRwDnNwqNFNGnaxZnEFB3CqcGMCGoloYfQXV0">
    <meta name="theme-color" content="#65d1a0">
    
    <link rel="preload" href="/assets/css/0.styles.6c1f7aaa.css" as="style"><link rel="preload" href="/assets/js/app.6d26e8d4.js" as="script"><link rel="preload" href="/assets/js/2.8273876b.js" as="script"><link rel="preload" href="/assets/js/50.aceb6009.js" as="script"><link rel="prefetch" href="/assets/js/10.cb095657.js"><link rel="prefetch" href="/assets/js/100.fbcd69a7.js"><link rel="prefetch" href="/assets/js/101.8fad45f8.js"><link rel="prefetch" href="/assets/js/102.16b0b3d1.js"><link rel="prefetch" href="/assets/js/103.c2af4d4a.js"><link rel="prefetch" href="/assets/js/104.d37d4660.js"><link rel="prefetch" href="/assets/js/105.27e54103.js"><link rel="prefetch" href="/assets/js/106.e4098682.js"><link rel="prefetch" href="/assets/js/107.8efeef5d.js"><link rel="prefetch" href="/assets/js/108.6c305bbf.js"><link rel="prefetch" href="/assets/js/109.6f1c6613.js"><link rel="prefetch" href="/assets/js/11.d1f39b42.js"><link rel="prefetch" href="/assets/js/110.6f264ba6.js"><link rel="prefetch" href="/assets/js/111.1b6294ef.js"><link rel="prefetch" href="/assets/js/112.2ad16154.js"><link rel="prefetch" href="/assets/js/113.a40acefc.js"><link rel="prefetch" href="/assets/js/114.d44cb107.js"><link rel="prefetch" href="/assets/js/115.bd15b919.js"><link rel="prefetch" href="/assets/js/116.778a9058.js"><link rel="prefetch" href="/assets/js/117.19ff15b7.js"><link rel="prefetch" href="/assets/js/118.2630ca8d.js"><link rel="prefetch" href="/assets/js/119.604bd439.js"><link rel="prefetch" href="/assets/js/12.892c0d9f.js"><link rel="prefetch" href="/assets/js/120.43a964f8.js"><link rel="prefetch" href="/assets/js/121.5993f55a.js"><link rel="prefetch" href="/assets/js/122.6c054fcf.js"><link rel="prefetch" href="/assets/js/123.95ddd9a3.js"><link rel="prefetch" href="/assets/js/124.c3a9510e.js"><link rel="prefetch" href="/assets/js/125.612c461a.js"><link rel="prefetch" href="/assets/js/126.ccdb194e.js"><link rel="prefetch" href="/assets/js/127.865789a0.js"><link rel="prefetch" href="/assets/js/128.9e0bc723.js"><link rel="prefetch" href="/assets/js/129.68515ee6.js"><link rel="prefetch" href="/assets/js/13.ea744146.js"><link rel="prefetch" href="/assets/js/130.32a31759.js"><link rel="prefetch" href="/assets/js/131.467e7aaf.js"><link rel="prefetch" href="/assets/js/132.3bb05df9.js"><link rel="prefetch" href="/assets/js/133.52bbf931.js"><link rel="prefetch" href="/assets/js/134.ee00e869.js"><link rel="prefetch" href="/assets/js/135.aadd30fb.js"><link rel="prefetch" href="/assets/js/136.6b5bb775.js"><link rel="prefetch" href="/assets/js/137.9d66b052.js"><link rel="prefetch" href="/assets/js/138.1df874a9.js"><link rel="prefetch" href="/assets/js/139.bd1e48e9.js"><link rel="prefetch" href="/assets/js/14.c755eadf.js"><link rel="prefetch" href="/assets/js/140.2a849d58.js"><link rel="prefetch" href="/assets/js/141.28c22cf9.js"><link rel="prefetch" href="/assets/js/142.e99735ee.js"><link rel="prefetch" href="/assets/js/143.0666fdce.js"><link rel="prefetch" href="/assets/js/144.216c1efe.js"><link rel="prefetch" href="/assets/js/145.fad2c93a.js"><link rel="prefetch" href="/assets/js/146.01533714.js"><link rel="prefetch" href="/assets/js/147.1af38e50.js"><link rel="prefetch" href="/assets/js/148.84844c3d.js"><link rel="prefetch" href="/assets/js/149.49ce06ed.js"><link rel="prefetch" href="/assets/js/15.7a29d3f4.js"><link rel="prefetch" href="/assets/js/150.234bdfef.js"><link rel="prefetch" href="/assets/js/151.89e0796a.js"><link rel="prefetch" href="/assets/js/152.389ced87.js"><link rel="prefetch" href="/assets/js/153.07285920.js"><link rel="prefetch" href="/assets/js/154.965fc5c1.js"><link rel="prefetch" href="/assets/js/155.7bf58a9a.js"><link rel="prefetch" href="/assets/js/156.99e400e4.js"><link rel="prefetch" href="/assets/js/157.b6edd5ab.js"><link rel="prefetch" href="/assets/js/158.af656e52.js"><link rel="prefetch" href="/assets/js/159.4b3d37d2.js"><link rel="prefetch" href="/assets/js/16.5c417580.js"><link rel="prefetch" href="/assets/js/160.fa1e9d1b.js"><link rel="prefetch" href="/assets/js/161.43d6f2b4.js"><link rel="prefetch" href="/assets/js/162.c0e7e60f.js"><link rel="prefetch" href="/assets/js/163.c78d27b4.js"><link rel="prefetch" href="/assets/js/164.7ee2986b.js"><link rel="prefetch" href="/assets/js/165.bbc6516b.js"><link rel="prefetch" href="/assets/js/166.90e15253.js"><link rel="prefetch" href="/assets/js/167.18578ca6.js"><link rel="prefetch" href="/assets/js/168.c52ad494.js"><link rel="prefetch" href="/assets/js/169.c34b2d53.js"><link rel="prefetch" href="/assets/js/17.76fdc3b2.js"><link rel="prefetch" href="/assets/js/170.c97f6096.js"><link rel="prefetch" href="/assets/js/171.358f0f02.js"><link rel="prefetch" href="/assets/js/172.30f03133.js"><link rel="prefetch" href="/assets/js/173.b72d5243.js"><link rel="prefetch" href="/assets/js/174.4e333643.js"><link rel="prefetch" href="/assets/js/175.2c07b836.js"><link rel="prefetch" href="/assets/js/176.29bcb24c.js"><link rel="prefetch" href="/assets/js/177.102b3f0f.js"><link rel="prefetch" href="/assets/js/178.5862bde0.js"><link rel="prefetch" href="/assets/js/179.0da54072.js"><link rel="prefetch" href="/assets/js/18.f857e697.js"><link rel="prefetch" href="/assets/js/180.393e8099.js"><link rel="prefetch" href="/assets/js/181.418f9fe8.js"><link rel="prefetch" href="/assets/js/182.e65424fa.js"><link rel="prefetch" href="/assets/js/183.3b3cb092.js"><link rel="prefetch" href="/assets/js/184.73f7a76a.js"><link rel="prefetch" href="/assets/js/185.537623fb.js"><link rel="prefetch" href="/assets/js/186.df802468.js"><link rel="prefetch" href="/assets/js/187.1095bafd.js"><link rel="prefetch" href="/assets/js/188.1d2acd1a.js"><link rel="prefetch" href="/assets/js/189.dc6a5186.js"><link rel="prefetch" href="/assets/js/19.8c1d0f81.js"><link rel="prefetch" href="/assets/js/190.a279b4dd.js"><link rel="prefetch" href="/assets/js/191.3abaa15f.js"><link rel="prefetch" href="/assets/js/192.4d0dd04e.js"><link rel="prefetch" href="/assets/js/193.2c698e6f.js"><link rel="prefetch" href="/assets/js/194.4243beaa.js"><link rel="prefetch" href="/assets/js/195.7c13b124.js"><link rel="prefetch" href="/assets/js/196.b18118bf.js"><link rel="prefetch" href="/assets/js/197.2abfac38.js"><link rel="prefetch" href="/assets/js/198.407f0a8b.js"><link rel="prefetch" href="/assets/js/199.d4e97578.js"><link rel="prefetch" href="/assets/js/20.47831bf7.js"><link rel="prefetch" href="/assets/js/200.86ce407c.js"><link rel="prefetch" href="/assets/js/201.56a65a3f.js"><link rel="prefetch" href="/assets/js/202.f852c6e8.js"><link rel="prefetch" href="/assets/js/203.92530d7a.js"><link rel="prefetch" href="/assets/js/204.ce612269.js"><link rel="prefetch" href="/assets/js/205.b14b2d70.js"><link rel="prefetch" href="/assets/js/206.b5112828.js"><link rel="prefetch" href="/assets/js/207.a4becd8a.js"><link rel="prefetch" href="/assets/js/208.f82e2766.js"><link rel="prefetch" href="/assets/js/209.11566ec8.js"><link rel="prefetch" href="/assets/js/21.8580b0b4.js"><link rel="prefetch" href="/assets/js/210.bf848e0e.js"><link rel="prefetch" href="/assets/js/211.6db52324.js"><link rel="prefetch" href="/assets/js/212.c9a03071.js"><link rel="prefetch" href="/assets/js/213.734c775f.js"><link rel="prefetch" href="/assets/js/214.7654a416.js"><link rel="prefetch" href="/assets/js/215.34ad7e3d.js"><link rel="prefetch" href="/assets/js/216.aabcd184.js"><link rel="prefetch" href="/assets/js/217.26ffe7d6.js"><link rel="prefetch" href="/assets/js/218.d8ceaf2e.js"><link rel="prefetch" href="/assets/js/219.82bad7ef.js"><link rel="prefetch" href="/assets/js/22.2d87ea68.js"><link rel="prefetch" href="/assets/js/220.0c35dcdf.js"><link rel="prefetch" href="/assets/js/221.8657d7c1.js"><link rel="prefetch" href="/assets/js/222.291af8a7.js"><link rel="prefetch" href="/assets/js/223.e8ce5ce0.js"><link rel="prefetch" href="/assets/js/224.2c3c1268.js"><link rel="prefetch" href="/assets/js/225.b31429da.js"><link rel="prefetch" href="/assets/js/226.8ec29e66.js"><link rel="prefetch" href="/assets/js/227.84469b41.js"><link rel="prefetch" href="/assets/js/228.cd6b9967.js"><link rel="prefetch" href="/assets/js/229.e5497c23.js"><link rel="prefetch" href="/assets/js/23.c1a929a6.js"><link rel="prefetch" href="/assets/js/230.643fdbb6.js"><link rel="prefetch" href="/assets/js/231.22457248.js"><link rel="prefetch" href="/assets/js/232.7e94e6f0.js"><link rel="prefetch" href="/assets/js/233.1083085e.js"><link rel="prefetch" href="/assets/js/234.6ce5972b.js"><link rel="prefetch" href="/assets/js/235.bc1d1e7e.js"><link rel="prefetch" href="/assets/js/236.f510d892.js"><link rel="prefetch" href="/assets/js/237.4690c583.js"><link rel="prefetch" href="/assets/js/238.ef69b085.js"><link rel="prefetch" href="/assets/js/239.e7cbaef4.js"><link rel="prefetch" href="/assets/js/24.707a9b9d.js"><link rel="prefetch" href="/assets/js/240.02e1ce4d.js"><link rel="prefetch" href="/assets/js/241.8ccdecd0.js"><link rel="prefetch" href="/assets/js/242.25a5eeac.js"><link rel="prefetch" href="/assets/js/243.80dc6cf3.js"><link rel="prefetch" href="/assets/js/244.d2d264c1.js"><link rel="prefetch" href="/assets/js/245.8646c059.js"><link rel="prefetch" href="/assets/js/246.0d788a8c.js"><link rel="prefetch" href="/assets/js/247.211b425b.js"><link rel="prefetch" href="/assets/js/248.15e5b430.js"><link rel="prefetch" href="/assets/js/249.b28c41d2.js"><link rel="prefetch" href="/assets/js/25.e47a8360.js"><link rel="prefetch" href="/assets/js/250.e4621bbf.js"><link rel="prefetch" href="/assets/js/251.2f49ea1d.js"><link rel="prefetch" href="/assets/js/252.c567824c.js"><link rel="prefetch" href="/assets/js/253.7eb2a6c2.js"><link rel="prefetch" href="/assets/js/254.adf8b2ad.js"><link rel="prefetch" href="/assets/js/255.43d07402.js"><link rel="prefetch" href="/assets/js/256.d49b01ff.js"><link rel="prefetch" href="/assets/js/257.cc61cd1a.js"><link rel="prefetch" href="/assets/js/258.e46ee926.js"><link rel="prefetch" href="/assets/js/259.5c84c742.js"><link rel="prefetch" href="/assets/js/26.db56ba8d.js"><link rel="prefetch" href="/assets/js/260.05cc8cff.js"><link rel="prefetch" href="/assets/js/261.1891f9c6.js"><link rel="prefetch" href="/assets/js/262.0913812a.js"><link rel="prefetch" href="/assets/js/263.a6836819.js"><link rel="prefetch" href="/assets/js/264.363da610.js"><link rel="prefetch" href="/assets/js/265.2cbfbe9f.js"><link rel="prefetch" href="/assets/js/266.555ca792.js"><link rel="prefetch" href="/assets/js/267.83f4c202.js"><link rel="prefetch" href="/assets/js/268.6dea35af.js"><link rel="prefetch" href="/assets/js/269.cbc5306b.js"><link rel="prefetch" href="/assets/js/27.0b0c1f7f.js"><link rel="prefetch" href="/assets/js/270.6bc430c5.js"><link rel="prefetch" href="/assets/js/271.f5105765.js"><link rel="prefetch" href="/assets/js/272.e1541ef7.js"><link rel="prefetch" href="/assets/js/273.158c7593.js"><link rel="prefetch" href="/assets/js/274.efb98c5b.js"><link rel="prefetch" href="/assets/js/275.cf2c3b21.js"><link rel="prefetch" href="/assets/js/276.0bf20a2d.js"><link rel="prefetch" href="/assets/js/277.2e4ddf6c.js"><link rel="prefetch" href="/assets/js/278.7f8b14ac.js"><link rel="prefetch" href="/assets/js/279.83a65d2f.js"><link rel="prefetch" href="/assets/js/28.a5d8ae18.js"><link rel="prefetch" href="/assets/js/280.47de1643.js"><link rel="prefetch" href="/assets/js/281.1d5cab51.js"><link rel="prefetch" href="/assets/js/282.b2299bce.js"><link rel="prefetch" href="/assets/js/283.fdad201b.js"><link rel="prefetch" href="/assets/js/284.cdf83f8f.js"><link rel="prefetch" href="/assets/js/285.caee6eb0.js"><link rel="prefetch" href="/assets/js/286.6b211023.js"><link rel="prefetch" href="/assets/js/287.e9e93db3.js"><link rel="prefetch" href="/assets/js/288.5892d2e1.js"><link rel="prefetch" href="/assets/js/289.48074c49.js"><link rel="prefetch" href="/assets/js/29.0bc8ea25.js"><link rel="prefetch" href="/assets/js/290.b478b2ab.js"><link rel="prefetch" href="/assets/js/291.18239d2b.js"><link rel="prefetch" href="/assets/js/292.9f6b48b4.js"><link rel="prefetch" href="/assets/js/293.70a0b924.js"><link rel="prefetch" href="/assets/js/294.bc5f021a.js"><link rel="prefetch" href="/assets/js/295.dfa72324.js"><link rel="prefetch" href="/assets/js/296.fb717477.js"><link rel="prefetch" href="/assets/js/297.2b2daf25.js"><link rel="prefetch" href="/assets/js/298.0ed3ce25.js"><link rel="prefetch" href="/assets/js/299.c59a3f82.js"><link rel="prefetch" href="/assets/js/3.d68e1d13.js"><link rel="prefetch" href="/assets/js/30.063f8dbe.js"><link rel="prefetch" href="/assets/js/300.f423c130.js"><link rel="prefetch" href="/assets/js/301.a11bcaa5.js"><link rel="prefetch" href="/assets/js/302.5a45b99e.js"><link rel="prefetch" href="/assets/js/303.80ac16da.js"><link rel="prefetch" href="/assets/js/304.f773afc2.js"><link rel="prefetch" href="/assets/js/305.52f1b3ba.js"><link rel="prefetch" href="/assets/js/306.966f2eef.js"><link rel="prefetch" href="/assets/js/307.c22143cf.js"><link rel="prefetch" href="/assets/js/308.a6dfec6e.js"><link rel="prefetch" href="/assets/js/309.377e4f0b.js"><link rel="prefetch" href="/assets/js/31.69d8f2c2.js"><link rel="prefetch" href="/assets/js/310.a76309fc.js"><link rel="prefetch" href="/assets/js/311.0706d5ca.js"><link rel="prefetch" href="/assets/js/312.40513d91.js"><link rel="prefetch" href="/assets/js/313.e36621af.js"><link rel="prefetch" href="/assets/js/314.3d4325be.js"><link rel="prefetch" href="/assets/js/315.72480163.js"><link rel="prefetch" href="/assets/js/316.83f31163.js"><link rel="prefetch" href="/assets/js/317.872fd713.js"><link rel="prefetch" href="/assets/js/318.0dc2aea4.js"><link rel="prefetch" href="/assets/js/319.454e4745.js"><link rel="prefetch" href="/assets/js/32.50aa541b.js"><link rel="prefetch" href="/assets/js/320.d9116236.js"><link rel="prefetch" href="/assets/js/321.b06a7a4e.js"><link rel="prefetch" href="/assets/js/322.41bcaaf1.js"><link rel="prefetch" href="/assets/js/323.f01ba88b.js"><link rel="prefetch" href="/assets/js/324.9a736f30.js"><link rel="prefetch" href="/assets/js/325.49b1193e.js"><link rel="prefetch" href="/assets/js/326.5a07b4a0.js"><link rel="prefetch" href="/assets/js/327.aceece56.js"><link rel="prefetch" href="/assets/js/328.e03489c3.js"><link rel="prefetch" href="/assets/js/329.5673fdef.js"><link rel="prefetch" href="/assets/js/33.4aa00ff3.js"><link rel="prefetch" href="/assets/js/330.246e74fa.js"><link rel="prefetch" href="/assets/js/331.e1cffa6b.js"><link rel="prefetch" href="/assets/js/332.2856b83e.js"><link rel="prefetch" href="/assets/js/333.d4c9c15a.js"><link rel="prefetch" href="/assets/js/334.13b03971.js"><link rel="prefetch" href="/assets/js/335.0b8ed702.js"><link rel="prefetch" href="/assets/js/336.3e71f28d.js"><link rel="prefetch" href="/assets/js/337.f929f08c.js"><link rel="prefetch" href="/assets/js/338.01a6cfbe.js"><link rel="prefetch" href="/assets/js/339.5cd647ad.js"><link rel="prefetch" href="/assets/js/34.b25c4a87.js"><link rel="prefetch" href="/assets/js/340.f941cae0.js"><link rel="prefetch" href="/assets/js/341.aa01a6b1.js"><link rel="prefetch" href="/assets/js/342.edd59a58.js"><link rel="prefetch" href="/assets/js/343.237c716b.js"><link rel="prefetch" href="/assets/js/344.06e59f2b.js"><link rel="prefetch" href="/assets/js/345.ddd4f6fe.js"><link rel="prefetch" href="/assets/js/346.70fe3c42.js"><link rel="prefetch" href="/assets/js/347.d39ad15e.js"><link rel="prefetch" href="/assets/js/348.1b135410.js"><link rel="prefetch" href="/assets/js/349.fd78d783.js"><link rel="prefetch" href="/assets/js/35.a1af8744.js"><link rel="prefetch" href="/assets/js/350.b10e7d69.js"><link rel="prefetch" href="/assets/js/351.a6ae4318.js"><link rel="prefetch" href="/assets/js/352.3117739a.js"><link rel="prefetch" href="/assets/js/353.99e022f1.js"><link rel="prefetch" href="/assets/js/354.a444c2d8.js"><link rel="prefetch" href="/assets/js/355.13d044aa.js"><link rel="prefetch" href="/assets/js/356.d2cfe5d0.js"><link rel="prefetch" href="/assets/js/357.eb44f05f.js"><link rel="prefetch" href="/assets/js/358.394c15db.js"><link rel="prefetch" href="/assets/js/359.6c541551.js"><link rel="prefetch" href="/assets/js/36.5558b717.js"><link rel="prefetch" href="/assets/js/360.65af88d5.js"><link rel="prefetch" href="/assets/js/361.d9b3c889.js"><link rel="prefetch" href="/assets/js/362.37ec0a7d.js"><link rel="prefetch" href="/assets/js/363.49690222.js"><link rel="prefetch" href="/assets/js/364.419b3dab.js"><link rel="prefetch" href="/assets/js/365.1f79df07.js"><link rel="prefetch" href="/assets/js/366.fa710c6f.js"><link rel="prefetch" href="/assets/js/367.497eeda5.js"><link rel="prefetch" href="/assets/js/368.45cf08f6.js"><link rel="prefetch" href="/assets/js/369.80a60b6a.js"><link rel="prefetch" href="/assets/js/37.511798d2.js"><link rel="prefetch" href="/assets/js/370.61b161b4.js"><link rel="prefetch" href="/assets/js/371.545f0249.js"><link rel="prefetch" href="/assets/js/372.d383f896.js"><link rel="prefetch" href="/assets/js/373.df22361b.js"><link rel="prefetch" href="/assets/js/374.0b9872ee.js"><link rel="prefetch" href="/assets/js/375.76bbca97.js"><link rel="prefetch" href="/assets/js/376.1f6f8210.js"><link rel="prefetch" href="/assets/js/377.3dd0dc75.js"><link rel="prefetch" href="/assets/js/378.32cac01d.js"><link rel="prefetch" href="/assets/js/379.6682a524.js"><link rel="prefetch" href="/assets/js/38.2dfd8d32.js"><link rel="prefetch" href="/assets/js/380.1ab9ec1d.js"><link rel="prefetch" href="/assets/js/381.cea4ec7d.js"><link rel="prefetch" href="/assets/js/382.fc51cce1.js"><link rel="prefetch" href="/assets/js/383.489718b4.js"><link rel="prefetch" href="/assets/js/384.a7a1c6d7.js"><link rel="prefetch" href="/assets/js/385.7b2d3a89.js"><link rel="prefetch" href="/assets/js/386.f0a18324.js"><link rel="prefetch" href="/assets/js/387.53063e56.js"><link rel="prefetch" href="/assets/js/388.1115759c.js"><link rel="prefetch" href="/assets/js/389.58bf8aaf.js"><link rel="prefetch" href="/assets/js/39.e8482eab.js"><link rel="prefetch" href="/assets/js/4.de17fbe4.js"><link rel="prefetch" href="/assets/js/40.c6dc3476.js"><link rel="prefetch" href="/assets/js/41.78bfe256.js"><link rel="prefetch" href="/assets/js/42.248cddd4.js"><link rel="prefetch" href="/assets/js/43.6429d3dc.js"><link rel="prefetch" href="/assets/js/44.71e9af05.js"><link rel="prefetch" href="/assets/js/45.f80fb4fc.js"><link rel="prefetch" href="/assets/js/46.3d5e7e58.js"><link rel="prefetch" href="/assets/js/47.1bbf5e9a.js"><link rel="prefetch" href="/assets/js/48.6909050b.js"><link rel="prefetch" href="/assets/js/49.9c775f82.js"><link rel="prefetch" href="/assets/js/5.59910585.js"><link rel="prefetch" href="/assets/js/51.acd0d321.js"><link rel="prefetch" href="/assets/js/52.a4f9f157.js"><link rel="prefetch" href="/assets/js/53.0f1ec943.js"><link rel="prefetch" href="/assets/js/54.6c5c2837.js"><link rel="prefetch" href="/assets/js/55.f412714f.js"><link rel="prefetch" href="/assets/js/56.64a3862c.js"><link rel="prefetch" href="/assets/js/57.d070eb7d.js"><link rel="prefetch" href="/assets/js/58.a80067c8.js"><link rel="prefetch" href="/assets/js/59.084f5e76.js"><link rel="prefetch" href="/assets/js/6.0ab71a58.js"><link rel="prefetch" href="/assets/js/60.42e0fbb5.js"><link rel="prefetch" href="/assets/js/61.b5b87c23.js"><link rel="prefetch" href="/assets/js/62.014cdbe2.js"><link rel="prefetch" href="/assets/js/63.ff1278d1.js"><link rel="prefetch" href="/assets/js/64.44a86cb2.js"><link rel="prefetch" href="/assets/js/65.1a69bd56.js"><link rel="prefetch" href="/assets/js/66.b8d9a1e9.js"><link rel="prefetch" href="/assets/js/67.48e3c0e6.js"><link rel="prefetch" href="/assets/js/68.127da596.js"><link rel="prefetch" href="/assets/js/69.77e4e1cb.js"><link rel="prefetch" href="/assets/js/7.769e6e3f.js"><link rel="prefetch" href="/assets/js/70.7863bd30.js"><link rel="prefetch" href="/assets/js/71.c5c89c65.js"><link rel="prefetch" href="/assets/js/72.030ac8a4.js"><link rel="prefetch" href="/assets/js/73.e96cf593.js"><link rel="prefetch" href="/assets/js/74.f9319fe7.js"><link rel="prefetch" href="/assets/js/75.049ce9e1.js"><link rel="prefetch" href="/assets/js/76.c71e5605.js"><link rel="prefetch" href="/assets/js/77.1e682453.js"><link rel="prefetch" href="/assets/js/78.0cce7473.js"><link rel="prefetch" href="/assets/js/79.3d32f303.js"><link rel="prefetch" href="/assets/js/8.812618a5.js"><link rel="prefetch" href="/assets/js/80.c956dc30.js"><link rel="prefetch" href="/assets/js/81.8276eaa8.js"><link rel="prefetch" href="/assets/js/82.10f6fdc6.js"><link rel="prefetch" href="/assets/js/83.cac65ec1.js"><link rel="prefetch" href="/assets/js/84.a4ce0ebf.js"><link rel="prefetch" href="/assets/js/85.d9efee67.js"><link rel="prefetch" href="/assets/js/86.cf64260c.js"><link rel="prefetch" href="/assets/js/87.e261e385.js"><link rel="prefetch" href="/assets/js/88.63b9894b.js"><link rel="prefetch" href="/assets/js/89.0ad95e30.js"><link rel="prefetch" href="/assets/js/9.c942e82a.js"><link rel="prefetch" href="/assets/js/90.77f9f729.js"><link rel="prefetch" href="/assets/js/91.3fcbf2d5.js"><link rel="prefetch" href="/assets/js/92.8b57334e.js"><link rel="prefetch" href="/assets/js/93.6095e56a.js"><link rel="prefetch" href="/assets/js/94.c616143f.js"><link rel="prefetch" href="/assets/js/95.a9b61cf9.js"><link rel="prefetch" href="/assets/js/96.a3896e7e.js"><link rel="prefetch" href="/assets/js/97.0318332c.js"><link rel="prefetch" href="/assets/js/98.17b8173b.js"><link rel="prefetch" href="/assets/js/99.d299c156.js">
    <link rel="stylesheet" href="/assets/css/0.styles.6c1f7aaa.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu have-body-img"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="仲灏小栈" class="logo"> <span class="site-name can-hide">仲灏小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/web/" class="nav-link">大前端</a></div><div class="nav-item"><a href="/backend-opt/" class="nav-link">后端&amp;运维</a></div><div class="nav-item"><a href="/other-tech/" class="nav-link">其他技术</a></div><div class="nav-item"><a href="/tech-live/" class="nav-link">生活</a></div><div class="nav-item"><a href="/about/" class="nav-link">关于我</a></div><div class="nav-item"><a href="/favorite/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/izhaong" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/izhaong/izhaong.com-oss/site-data/avatar/avatar-wx.jpg"> <div class="blogger-info"><h3>仲灏</h3> <span>诚意, 正心, 格物, 致知</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/web/" class="nav-link">大前端</a></div><div class="nav-item"><a href="/backend-opt/" class="nav-link">后端&amp;运维</a></div><div class="nav-item"><a href="/other-tech/" class="nav-link">其他技术</a></div><div class="nav-item"><a href="/tech-live/" class="nav-link">生活</a></div><div class="nav-item"><a href="/about/" class="nav-link">关于我</a></div><div class="nav-item"><a href="/favorite/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/izhaong" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/note/infrastructure/" class="sidebar-link">《前端项目基础建设》</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>HTML&amp;CSS</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>JavaScript&amp;TypeScript</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Node</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/febcd5/" class="sidebar-link">nvm 使用</a></li><li><a href="/pages/b378af/" class="sidebar-link">nvm 深度使用解析</a></li><li><a href="/pages/96c6f8/" class="sidebar-link">pnpm 学习</a></li><li><a href="/pages/8743eb/" class="sidebar-link">pnpm 使用及问题处理</a></li><li><a href="/pages/d15ce2/" class="sidebar-link">npm yarn pnpm命令对比</a></li><li><a href="/pages/037850/" class="sidebar-link">koa学习</a></li><li><a href="/pages/3e0a1c/" class="sidebar-link">restful api笔记</a></li><li><a href="/pages/1210/" class="sidebar-link">Egg.js 技巧</a></li><li><a href="/pages/9da7c4/" class="sidebar-link">Sequelize 学习笔记</a></li><li><a href="/pages/110928/" class="sidebar-link">npm 使用记录</a></li><li><a href="/pages/50e782/" class="sidebar-link">node 记错修复</a></li><li><a href="/pages/34d0c9/" class="sidebar-link">npm 仓库</a></li><li><a href="/pages/30de9a/" class="sidebar-link">安装npm的过程中发生了什么</a></li><li><a href="/pages/5565db/" class="sidebar-link">想用而找不到的 npm 插件</a></li><li><a href="/pages/f3d9dd/" aria-current="page" class="active sidebar-link">node全局脚本原理解析</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/f3d9dd/#dedent库" class="sidebar-link">dedent库</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level5"><a href="/pages/f3d9dd/#parse" class="sidebar-link">parse</a></li><li class="sidebar-sub-header level6"><a href="/pages/f3d9dd/#npm本地包引用方法" class="sidebar-link">npm本地包引用方法</a></li><li class="sidebar-sub-header level3"><a href="/pages/f3d9dd/#项目初始化" class="sidebar-link">项目初始化</a></li><li class="sidebar-sub-header level3"><a href="/pages/f3d9dd/#创建package" class="sidebar-link">创建package</a></li><li class="sidebar-sub-header level3"><a href="/pages/f3d9dd/#发布前的准备" class="sidebar-link">发布前的准备</a></li><li class="sidebar-sub-header level3"><a href="/pages/f3d9dd/#发布" class="sidebar-link">发布</a></li></ul></li></ul></li><li><a href="/pages/e7db5c/" class="sidebar-link">转-commander</a></li><li><a href="/pages/34a68b/" class="sidebar-link">制作自己的npm包</a></li><li><a href="/pages/147a7a/" class="sidebar-link">lerna</a></li><li><a href="/pages/8c0e85/" class="sidebar-link">lerna - Lerna an Nx</a></li><li><a href="/pages/6bc0b9/" class="sidebar-link">lerna - 功能点</a></li><li><a href="/pages/f377d0/" class="sidebar-link">lerna API参考</a></li><li><a href="/pages/3de397/" class="sidebar-link">Egg.js 技巧 问题集合</a></li><li><a href="/pages/efacd5/" class="sidebar-link">Node+vue打造全栈资源分享网站</a></li><li><a href="/pages/a97403/" class="sidebar-link">脚手架核心流程开发</a></li><li><a href="/pages/e20e84/" class="sidebar-link">Untitled</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>构建</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Vue</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>React</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>小程序</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>跨端</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Electron</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>WebGL&amp;GIS</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>浏览器</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面经</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>其他</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/web/#大前端" data-v-06225672>大前端</a></li><li data-v-06225672><a href="/web/#Node" data-v-06225672>Node</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/izhaong" target="_blank" title="作者" class="beLink" data-v-06225672>仲灏</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2022-08-28</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">node全局脚本原理解析<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="脚手架实现原理"><a href="#脚手架实现原理" class="header-anchor">#</a> 脚手架实现原理</h1> <h5 id="为什么全局安装-vue-cli后会添加vue的命令"><a href="#为什么全局安装-vue-cli后会添加vue的命令" class="header-anchor">#</a> 为什么全局安装@vue/cli后会添加vue的命令？</h5> <div class="language- line-numbers-mode"><pre class="language-text"><code>npm install -g @vue/cli
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>查看vue实际文件路径</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; which vue
/usr/local/bin/vue
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>bin目录下存放的是可执行文件</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; cd /usr/local/bin
&gt; ll
lrwxr-xr-x  1 song  admin    39B 12 28 21:48 vue -&gt; ../lib/node_modules/@vue/cli/bin/vue.js
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>可以看到vue实际是一个软链接，指向：../lib/node_modules/@vue/cli/bin/vue.js</p> <h5 id="绑定管理在哪里指定的呢"><a href="#绑定管理在哪里指定的呢" class="header-anchor">#</a> 绑定管理在哪里指定的呢？</h5> <p>进入到@vue/cli安装目录</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; cd /usr/local/lib/node_modules/@vue/cli
&gt; ll
-rw-r--r--    1 song  admin   2.5K 12 28 21:48 package.json
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>在package.json中有一个bin的配置</p> <div class="language- line-numbers-mode"><pre class="language-text"><code> &quot;bin&quot;: {
    &quot;vue&quot;: &quot;bin/vue.js&quot;
  },
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>这里配置了安装完之后的软链接名称，以及指向的实际文件</p> <h5 id="全局安装-vue-cli时发生了什么"><a href="#全局安装-vue-cli时发生了什么" class="header-anchor">#</a> 全局安装@vue/cli时发生了什么？</h5> <p>npm install -g @vue/cli</p> <ul><li>第一步：会把@vue/cli下载到node node_modules中</li> <li>第二步：下载成功后会解析package.json 中的 bin 配置，有这个配置就会创建一个软链接</li></ul> <h5 id="vue执行一个js文件-为什么可以执行它"><a href="#vue执行一个js文件-为什么可以执行它" class="header-anchor">#</a> vue执行一个js文件，为什么可以执行它？</h5> <ul><li><p>执行vue命令时，系统会执行which vue在环境变量中找vue的注册并执行文件</p> <div class="language- line-numbers-mode"><pre class="language-text"><code># 这两条命令执行是等价的
&gt; vue
&gt;/usr/local/bin/vue
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></li> <li><p>执行的真实文件是vue对应的软链接：../lib/node_modules/@vue/cli/bin/vue.js</p> <p>直接执行一个xx.js执行不了的，vue.js又是怎么执行的呢？</p> <p>js文件需要一个解释器(node)来执行 vue.js源码第一行</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>#!/usr/bin/env node
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>自己创建一个js文件,test.js中第一行加入此代码，通过 ./test.js也能直接执行。</p> <p>为什么能直接执行？</p> <p>这句话的意思是，告诉系统在环境变量中去找node命令，来执行此文件</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; /usr/bin/env node # 会将node命令执行起来，与直接执行node是一样的效果
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>所以./test.js等于 /usr/bin/env node test.js 等于 node test.js</p> <p>chmod 777 test.js 设置文件为可执行文件</p> <h5 id="自定义一条命令"><a href="#自定义一条命令" class="header-anchor">#</a> 自定义一条命令</h5> <p>思路：在环境变量中创建一个软链接，执行 test.js即可 (软链接可以嵌套) 在 /usr/local/bin下执行</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt;  ln -s &lt;路径&gt;/test/index.js &lt;name&gt; #删除软链接 rm &lt;name&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul> <h1 id="脚手架的开发流程"><a href="#脚手架的开发流程" class="header-anchor">#</a> 脚手架的开发流程</h1> <ul><li><p>创建npm项目</p></li> <li><p>创建脚手架入口文件，最上方添加：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>#!/usr/bin/env node
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>配置package.json 添加bin属性</p></li> <li><p>编写脚手架代码</p></li> <li><p>将脚手架发布到npm</p></li></ul> <h3 id="脚手架开发难点"><a href="#脚手架开发难点" class="header-anchor">#</a> 脚手架开发难点</h3> <ul><li><p>分包： 将复杂系统拆分成若干个模块</p></li> <li><p>命令注册：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>vue create
vue add 
vue invoke
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></li> <li><p>参数解析</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>vue command [options] &lt;params&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>options全称：--version、--help options简写： -V、-h</p></li> <li><p>帮助文档</p></li> <li><p>命令行交互</p></li> <li><p>日志打印</p></li> <li><p>命令行文字变色</p></li> <li><p>网络通信：HTTP/WebSocket</p></li> <li><p>文件处理 ....</p></li></ul> <p><strong>理解npm link</strong></p> <ul><li><em>npm link your-lib</em>：将当前项目中 <em>node_modules</em> 下指定的库文件链接到 <em>node</em>全局<em>node_modules</em>下的库文件</li> <li><em>npm link</em>: 将当前项目链接到<em>node</em>全局<em>node_modules</em>中作为一个库文件，并解析<em>bin</em>配置创建可执行文件</li></ul> <p><strong>理解npm unlink</strong></p> <ul><li><em>npm unlink</em>：将当前项目从<em>node</em>全局<em>node_modules</em>中移除</li> <li><em>npm unlink your-li</em>：将当前项目中的库文件依赖移除</li></ul> <h3 id="创建一个脚手架"><a href="#创建一个脚手架" class="header-anchor">#</a> 创建一个脚手架</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; mkdir cli-test # 创建一个文件夹
&gt; npm init -y # 初始化
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>cli-test 目录: -- package.json -- bin</p> <ul><li>|-- inde.js</li></ul> <p>index.js 文件:</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>#!/usr/bin/env node
console.log('Hello cli')
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>package.json文件：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>{
  ...
  &quot;main&quot;: &quot;index.js&quot;,
  &quot;bin&quot;:  {
        &quot;cli-test&quot;: &quot;bin/index.js&quot;
    }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>将脚手架发布到npm</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; npm login # 登录npm
&gt; npm publish # 发布
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>在cli-test目录下，进行全局安装 <em>npm install -g cli-test</em> 就会建立一个软链接，方便进行本地调试， 通过<em>npm link</em> 也可。</p> <h1 id="yargs入门"><a href="#yargs入门" class="header-anchor">#</a> yargs入门</h1> <p>Yargs通过解析参数来帮助您构建脚手架的工具。</p> <h3 id="通过yargs创建一个最简单的脚手架工具"><a href="#通过yargs创建一个最简单的脚手架工具" class="header-anchor">#</a> 通过yargs创建一个最简单的脚手架工具</h3> <p>定义文件<em>index.js</em></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>#!/usr/bin/env node

const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')

const arg = hideBin(process.argv)
yargs(arg)
     .argv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>执行</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; ./index.js --help
选项：
  --help     显示帮助信息                                                 [布尔]
  --version  显示版本号                                                   [布尔]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="严格模式-strict"><a href="#严格模式-strict" class="header-anchor">#</a> 严格模式: <em>strict</em></h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>yargs(arg)
    .strict()
    .argv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>加入strict，如果有无法识别的参数，将会给出提示</p> <h3 id="用法提示-usage"><a href="#用法提示-usage" class="header-anchor">#</a> 用法提示: <em>usage</em></h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>yargs(arg)
    .usage('Usage: test [command] &lt;options&gt;')
    .strict()
    .argv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>使用--help将会打印出使用信息</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; ./index.js --help
test [command] &lt;options&gt;

选项：
  --help     显示帮助信息                                                 [布尔]
  --version  显示版本号
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="最少输入的command个数-demandcommand"><a href="#最少输入的command个数-demandcommand" class="header-anchor">#</a> 最少输入的command个数: <em>demandCommand</em></h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>yargs(arg)
    .usage('Usage: test [command] &lt;options&gt;')
    .demandCommand(1, '最少输入一个参数')
    .strict()
    .argv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="设置command别名-alias"><a href="#设置command别名-alias" class="header-anchor">#</a> 设置command别名: <em>alias</em></h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>yargs(arg)
    .usage('Usage: test [command] &lt;options&gt;')
    .demandCommand(1, '最少输入一个参数')
    .strict()
    .alias('h', 'help') //-h 和  --help 效果一样
    .argv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="设置输出内容的宽度-wrap"><a href="#设置输出内容的宽度-wrap" class="header-anchor">#</a> 设置输出内容的宽度: <em>wrap</em></h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>const cli = yargs(arg)
cli.usage('Usage: test [command] &lt;options&gt;')
    .demandCommand(1, '最少输入一个参数')
    .strict()
    .alias('h', 'help')
    .wrap(cli.terminalWidth()) // terminalWidth返回当前窗口的宽度
    .argv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="设置结尾显示的内容-epilogue"><a href="#设置结尾显示的内容-epilogue" class="header-anchor">#</a> 设置结尾显示的内容: <em>epilogue</em></h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>cli.usage('Usage: test [command] &lt;options&gt;')
    .demandCommand(1, '最少输入一个参数')
    .strict()
    .alias('h', 'help')
    .wrap(cli.terminalWidth())
    .epilogue('结尾显示的话')
    .argv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="为全局command添加选项-options"><a href="#为全局command添加选项-options" class="header-anchor">#</a> 为全局command添加选项: <em>options</em></h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>cli.usage('Usage: test [command] &lt;options&gt;')
    .alias('h', 'help')
    .options({
        debug: { // 添加的选项名
            type: 'boolean',
            describe: 'debug mode',
            alias: 'd'  // 别名
        }
    })
    .argv
// options('name', {}) 一个一个设置的用法
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>执行效果</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; ./index.js -h
Usage: test [command] &lt;options&gt;

选项：
      --version  显示版本号                                               [布尔]
  -d, --debug    debug mode                                              [布尔]
  -h, --help     显示帮助信息                                             [布尔]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="将选项分组-group"><a href="#将选项分组-group" class="header-anchor">#</a> 将选项分组: <em>group</em></h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>cli.usage('Usage: test [command] &lt;options&gt;')
    .alias('h', 'help')
    .options({
        debug: {
            type: 'boolean',
            describe: 'debug mode',
            alias: 'd'
        }
    })
    .group(['debug'], 'Dev Options:')
    .argv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>执行效果</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; ./index.js -h
Usage: test [command] &lt;options&gt;

Dev Options:
  -d, --debug  debug mode                                                [布尔]

选项：
      --version  显示版本号                                               [布尔]
  -h, --help     显示帮助信息                                             [布尔]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="命令纠错提示-recommendcommands"><a href="#命令纠错提示-recommendcommands" class="header-anchor">#</a> 命令纠错提示：<em>recommendCommands()</em></h3> <p>会根据当前输入的command去找最相似的进行提示</p> <h3 id="自定义错误信息-fail-err-msg"><a href="#自定义错误信息-fail-err-msg" class="header-anchor">#</a> 自定义错误信息: <em>fail((err,msg) =&gt; {...})</em></h3> <h2 id="dedent库"><a href="#dedent库" class="header-anchor">#</a> dedent库</h2> <p>去除每行顶部空格，方便多行字符串的输出</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>const dedent = require('dedent')
console.log(dedent`
    第一行，
    第二行
`)
// 将会订购显示输出
/**
第一行，
第二行
*/
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h1 id="自定义命令"><a href="#自定义命令" class="header-anchor">#</a> 自定义命令</h1> <p>官方示例</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')

yargs(hideBin(process.argv))
    .command(
        'serve [port]', // serve 脚手架后面输入的名，[port]定义的option
        'start the server', // 描述
        (yargs) =&gt; { //builder，在执行这个command之前做的事情
            yargs
                .positional('port', {
                    describe: 'port to bind on',
                    default: 5000
                })
        }, 
        (argv) =&gt; { // handler，执行comand 的行为
            if (argv.verbose) console.info(`start server on :${argv.port}`)
            serve(argv.port)
        }
    )
    .option('verbose', {
        alias: 'v',
        type: 'boolean',
        description: 'Run with verbose logging'
    })
    .argv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><p>自定义</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>cli.usage('Usage: test [command] &lt;options&gt;')
    .alias('h', 'help')
    .options({
        debug: {
            type: 'boolean',
            describe: 'debug mode',
            alias: 'd'
        }
    })
    .group(['debug'], 'Dev Options:')
    .command('init [name]', '初始化的命令', (yargs) =&gt; {
        yargs.option('name', {
            type: 'string',
            describe: 'init的option',
            alias: 'n'
        })
    }, (argv) =&gt; {
        console.log(argv)
    })
    .argv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p>执行效果</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; ./index.js init  
{ _: [ 'init' ], '$0': 'index.js' }

&gt; ./index.js init -h 
ndex.js init [name]

初始化的命令

Dev Options:
  -d, --debug  debug mode                                                 [布尔]

选项：
      --version  显示版本号                                               [布尔]
  -n, --name     init的option                                           [字符串]
  -h, --help     显示帮助信息                                             [布尔]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>使用对象方式定义</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>    ...
    .command({
        command: 'list',
        aliases: ['ls', 'la', 'll'],
        describe: 'list 的描述',
        builder: (yargs) =&gt; {

        },
        handler: (argv) =&gt; {
            console.log(argv)
        }
    })
    .argv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h5 id="parse"><a href="#parse" class="header-anchor">#</a> parse</h5> <p>解析命令参数，合并传入的参数，合并完作为一个新的参数注入到脚手架中</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>#!/usr/bin/env node

const yargs = require('yargs/yargs')
const pkg = require('../package.json')

const argv = process.argv.splice(2)
const context = {
    testVersion: pkg.version
}

yargs()
    .command({
        command: 'list',
        aliases: ['ls', 'la', 'll'],
        describe: 'list 的描述',
        builder: (yargs) =&gt; {},
        handler: (argv) =&gt; {
            console.log(argv)
        }
    })
    .parse(argv, context)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>执行效果</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; ./index.js list
{ _: [ 'list' ], testVersion: '1.0.5', '$0': 'index.js' }
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h1 id="lerna初始化过程"><a href="#lerna初始化过程" class="header-anchor">#</a> Lerna初始化过程</h1> <p><img src="/Users/izhaong/izhaong/Project_me/Blog/izhaong.com-localFile/1030101.node%E5%85%A8%E5%B1%80%E8%84%9A%E6%9C%AC%E5%8E%9F%E7%90%86%E8%A7%A3%E6%9E%90/1240.jpeg" alt="img"></p> <h6 id="npm本地包引用方法"><a href="#npm本地包引用方法" class="header-anchor">#</a> npm本地包引用方法</h6> <p>除了npm link 还可以通过<em>file:</em></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>@lerna/global-options:&quot;file:../global-options&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h1 id="lerna创建发布流程"><a href="#lerna创建发布流程" class="header-anchor">#</a> Lerna创建发布流程</h1> <h3 id="项目初始化"><a href="#项目初始化" class="header-anchor">#</a> 项目初始化</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; mkdir my-cli-dev # 创建项目文件
&gt; npm init -y # 在项目目录下初始化
&gt; npm i -g lerna # 全局安装lerna
&gt; lerna init # 初始化
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="创建package"><a href="#创建package" class="header-anchor">#</a> 创建package</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; lerna create core # package name 为 @my-cli-dev/core
&gt; lerna create utils # package name 为 @my-cli-dev/utils
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>package.json中name为@my-cli-dev/core这种方式，my-cli-dev则为组织名称，需要在npm上创建一个对应的组织，可以避免名字的重复。若包发布不上去检查下这个组织是否已经建立。 core/package.json 中dependencies 添加@my-cli-dev/utils的依赖。 通过<em>lerna link</em>链接到本地库</p> <h3 id="发布前的准备"><a href="#发布前的准备" class="header-anchor">#</a> 发布前的准备</h3> <p><strong>1、创建git仓库</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; git remote add origin https://xx/cli.git # 添加仓库的链接
# 代码提交到仓库
&gt; git add .
&gt; git commit -m 'init' 
&gt; git push origin master --set-upstream
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>2、需要npm login</strong> <strong>3、根目录下添加LICENSE.md文件</strong> <strong>4、package.json中添加publishConfig设置为公有库</strong></p> <h3 id="发布"><a href="#发布" class="header-anchor">#</a> 发布</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt; lerna publish
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>错误问题</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>lerna ERR! E403 [no_perms] Private mode enable, only admin can publish this module
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>出现原因：使用的是淘宝源cnpm,登陆到的是cnpm 解决方法：切换到npmjs的网址，代码如下 npm config set registry http://registry.npmjs.org/ 切换过去之后记得npm login</p></div></div>  <div class="page-edit"><!----> <!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2022/10/26, 16:57:52</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/5565db/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">想用而找不到的 npm 插件</div></a> <a href="/pages/e7db5c/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">转-commander</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/5565db/" class="prev">想用而找不到的 npm 插件</a></span> <span class="next"><a href="/pages/e7db5c/">转-commander</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/e3ef00/"><div>
            接雨水
            <!----></div></a> <span class="date">04-08</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/a5eeb6/"><div>
            vim日常使用记录
            <!----></div></a> <span class="date">04-08</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/d46779/"><div>
            有效的字母异位词
            <!----></div></a> <span class="date">04-08</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:izhaong@outlook.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/izhaong" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2021-2024
    <span>izhaong | <a href="https://github.com/izhaong" target="_blank">github</a> | <a href="http://beian.miit.gov.cn/" target="_blank">蜀ICP备2021031194号</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <div class="body-bg" style="background:url() center center / cover no-repeat;opacity:0.5;"></div> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.6d26e8d4.js" defer></script><script src="/assets/js/2.8273876b.js" defer></script><script src="/assets/js/50.aceb6009.js" defer></script>
  </body>
</html>
